package com.wyix.demo;

import com.wyix.entity.Order;
import com.wyix.error.BaseBusinessException;
import com.wyix.error.InvalidOrderException;
import com.wyix.error.OrderNotFoundException;
import com.wyix.service.OrderService;

import java.io.IOException;

/**
 * @author wangdx
 * @since 2025/8/1
 */
public class ExceptionDemo {
    public static void main(String[] args) {
        OrderService orderService = new OrderService();

        // 场景1：查询不存在的订单（触发OrderNotFoundException）
        try {
            orderService.getOrderById("NOT_EXIST");
        } catch (OrderNotFoundException e) {
            System.out.println("场景1处理结果：" + e.getMessage() + "（错误码：" + e.getErrorCode() + "）");
        }

        // 场景2：创建无效订单（金额为负，触发InvalidOrderException）
        try {
            Order invalidOrder = new Order("TEST_002", "AirPods", -1000);
            orderService.createOrder(invalidOrder);
        } catch (InvalidOrderException e) {
            System.out.println("场景2处理结果：" + e.getMessage());
        }

        // 场景3：调用importOrdersFromFile（需处理受检异常IOException）
        try {
            orderService.importOrdersFromFile("invalid_orders.txt"); // 文件不存在
        } catch (IOException e) {
            System.out.println("场景3处理结果：文件导入失败：" + e.getMessage());
        }

        // 场景4：保存订单到数据库（触发异常链）
        try {
            Order order = new Order("TEST_003", "MacBook", 15999);
            orderService.saveOrderToDB(order);
        } catch (BaseBusinessException e) {
            System.out.println("场景4处理结果：" + e.getMessage());
            System.out.println("原始异常：" + e.getCause().getMessage()); // 获取异常链中的底层异常
        }
    }
}
